package com.caishi.lkx.exam.model;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zzw.common.entity.impl.BSEntity;
import com.caishi.lkx.exam.dto.QuestionSourceDto;
import com.caishi.lkx.exam.ienum.type.QuestionDifficulty;
import com.caishi.lkx.exam.ienum.type.QuestionSourceType;
import com.caishi.lkx.exam.ienum.type.QuestionType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.caishi.lkx.exam.util.QuestionUtil.sensitiveContent;

/**
 * @author by Jansia
 * @Date 2022/1/7 15:06
 * @Description
 */
@Schema(title = "试题实体类")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "ex_question", autoResultMap = true)
public class QuestionModel extends BSEntity<QuestionModel> implements CstkModel {

    @TableLogic(delval = "1", value = "0")
    private Boolean deleted;

    @Schema(title = "学科id")
    private Long categoryId;

    @Schema(title = "题型")
    private QuestionType firstType;

    @Schema(title = "题型id")
    private String typeId;

    @Schema(title = "知识梯度")
    private Integer knowledgeLevel;

    @Schema(title = "难易度")
    private QuestionDifficulty difficulty;

    @Schema(title = "资源类型")
    private QuestionSourceType sourceType;

    @Schema(title = "源id")
    private String sourceId;

    @Schema(title = "题干")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> stem;

    @Schema(title = "选项")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> quOption;

    @Schema(title = "题干")
    @TableField(typeHandler = FastjsonTypeHandler.class, exist = false)
    private List<Map<String, Object>> stemHigh;

    @Schema(title = "选项")
    @TableField(typeHandler = FastjsonTypeHandler.class, exist = false)
    private List<Map<String, Object>> quOptionHigh;


    @Schema(title = "答案解析")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<Map<String, Object>> answerDesc;

    @Schema(title = "质量梯度")
    private BigDecimal qualityLevel;

    @Schema(title = "试题分数")
    private BigDecimal score;

    @Schema(title = "是否被隐藏")
    private Boolean isHide;

    @Schema(title = "所属平台")
    private String platformCode;

    @Schema(hidden = true)
    @TableField(exist = false)
    private List<QuestionModel> children;

    @JsonIgnore
    @TableField("answer")
    private String rightAnswerStr;

    @Schema(title = "答案")
    @TableField(exist = false)
    private Object answer;

//    expand数据

    @Schema(title = "知识点")
    @TableField(exist = false)
    private String knowledgePoint;

    @Schema(title = "试题签名")
    @TableField(exist = false)
    private String sign;

    @Schema(title = "题型名称")
    @TableField(exist = false)
    private String type;

    @TableField(exist = false)
    @Schema(title = "试题所在的试卷和章节")
    private QuestionSourceDto questionSource;

    public void setAnswer(Object obj) {
        this.answer = obj;
        if (obj == null) {
            return;
        }
        this.rightAnswerStr = JSONUtil.toJsonStr(answer);
    }

    public void setRightAnswerStr(String rightAnswerStr) {
        if (StrUtil.isBlank(rightAnswerStr)) {
            return;
        }
        if (JSONUtil.isJson(rightAnswerStr)) {
            this.answer = JSONUtil.parse(rightAnswerStr);
        }else{
            this.answer = rightAnswerStr;
        }


    }

    @Override
    public String getCstkId() {
        if (sourceType == QuestionSourceType.inputForC) {
            return sourceId;
        }
        return null;
    }

    @Override
    public void setCstkId(String id) {
        sourceType = QuestionSourceType.inputForC;
        sourceId = id;
    }

    @TableField(exist = false)
    private Integer sort;

    public List<Map<String, Object>> getStem() {
        return sensitiveContent(this.stem);
    }


    public List<Map<String, Object>> getQuOption() {
        return sensitiveContent(this.quOption);
    }


    public List<Map<String, Object>> getAnswerDesc() {
        return sensitiveContent(this.answerDesc);
    }


}

